/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Datos;

import Entidades.Ventas;
import Interfaces.IVentas;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author HOUSE
 */
public class DBVentas implements IVentas{
private Connection conexion = null;
    public Connection getConection() {
       if (conexion == null) {
           try {
               Driver driver = new com.mysql.jdbc.Driver();
               DriverManager.registerDriver(driver);
               conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/automotora", "root", "pass1234");
              
           } catch (SQLException ex) {
               ex.printStackTrace();
               Logger.getLogger(DBAutos.class.getName()).log(Level.SEVERE, null, ex);

           }
       }
       return conexion;
   }
    @Override
    public boolean IngresaVentas(Ventas nodo) {
        try {
           CallableStatement statement;
           statement = getConection().prepareCall("{call IngresaVentas (?,?,?,?,?,?,?)}");         
           statement.setInt(1, nodo.getIDVenta());
           statement.setInt(2, nodo.getNroUnidad());
           statement.setInt(3, nodo.getIDVendedor());
           statement.setInt(4,nodo.getIDCliente());
           statement.setString(5,nodo.getFechaVenta());
           statement.setInt(6,nodo.getImporteVenta());
           statement.setInt(7, nodo.getComision());
           
           
         
           statement.executeUpdate();
        }catch (Exception ex){
            System.out.println("error al ingresar mecanicos");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean ModificaVentas(Ventas nodo) {
        try {
           CallableStatement statement;
           statement = getConection().prepareCall("{call ModificaVentas (?,?,?,?,?,?,?)}");         
           statement.setInt(1, nodo.getIDVenta());
           statement.setInt(2, nodo.getNroUnidad());
           statement.setInt(3, nodo.getIDVendedor());
           statement.setInt(4,nodo.getIDCliente());
           statement.setString(5,nodo.getFechaVenta());
           statement.setInt(6,nodo.getImporteVenta());
           statement.setInt(7, nodo.getComision());
           
           
         
           statement.executeUpdate();
        }catch (Exception ex){
            System.out.println("error al ingresar mecanicos");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean EliminaVentas(int IDVentas) {
         try {
            CallableStatement statement;
            statement = getConection().prepareCall("{call EliminaVentas(?)}");
            statement.setInt(1, IDVentas);
            statement.executeUpdate();


        } catch (Exception ex) {
            System.out.println("error al eliminar vendedor");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public ArrayList<Ventas> ListaVentas() {
        ArrayList<Ventas> per = new ArrayList<Ventas>();
       try {
           CallableStatement statement = getConection().prepareCall("{call ListaVentas}");
           ResultSet resultSet = statement.executeQuery();
           while (resultSet.next()) {
               Ventas reglam= new Ventas();
              
               reglam.setIDVenta(resultSet.getInt(1));
               reglam.setNroUnidad(resultSet.getInt(2));
               reglam.setIDVendedor(resultSet.getInt(3));
               reglam.setIDCliente(resultSet.getInt(4));
               reglam.setFechaVenta(resultSet.getString(5));
               reglam.setImporteVenta(resultSet.getInt(6));
               reglam.setComision(resultSet.getInt(7));
               
               per.add(reglam);
              
           }
           return per;
       } catch (SQLException ex) {
           ex.printStackTrace();
           return null;
       }
    }

    @Override
    public DefaultTableModel ModeloVentas() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
